<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<!--				<a-col :span="6">-->
				<!--					<a-form-item label="审批状态" name="approvalStatus">-->
				<!--						<a-select v-model:value="searchFormState.approvalStatus" placeholder="请选择审批状态">-->
				<!--							<a-select-option value="APPLICANT_DEPT">本部门出差审批</a-select-option>-->
				<!--							<a-select-option value="VISIT_DEPT">来访人员审批</a-select-option>-->
				<!--						</a-select>-->
				<!--					</a-form-item>-->
				<!--				</a-col>-->
				<a-col :span="6">
					<a-form-item label="申请人姓名" name="applicantName">
						<a-input v-model:value="searchFormState.applicantName" placeholder="请输入申请人姓名" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="申请人部门" name="applicantDeptName">
						<a-input v-model:value="searchFormState.applicantDeptName" placeholder="请输入申请人部门" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="到访部门" name="visitDeptName">
						<a-input v-model:value="searchFormState.visitDeptName" placeholder="请输入到访部门" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="到访午餐食堂" name="visitLunchCafeteriaName">
						<a-input v-model:value="searchFormState.visitLunchCafeteriaName" placeholder="请输入到访午餐食堂" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="到访晚餐食堂" name="visitSupperCafeteriaName">
						<a-input v-model:value="searchFormState.visitSupperCafeteriaName" placeholder="请输入到访晚餐食堂" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="到访时间" name="visitTime">
						<a-range-picker v-model:value="searchFormState.visitTime" value-format="YYYY-MM-DD HH:mm:ss" show-time />
					</a-form-item>
				</a-col>

				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'" />
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<!--					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizApprovalAdd')">-->
					<!--						<template #icon><plus-outlined /></template>-->
					<!--						新增-->
					<!--					</a-button>-->
					<xn-batch-button
						v-if="hasPerm('bizApprovalBatchDelete')"
						buttonName="批量删除"
						icon="DeleteOutlined"
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchBizApproval"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'approvalStatus'">
					{{ $TOOL.dictTypeData('APPROVAL_STATUS', record.approvalStatus) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizApprovalEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['bizApprovalEdit', 'bizApprovalDelete'], 'and')" />
						<a @click="drawerOpenClick(record)">日志</a>
						<a-divider type="vertical" v-if="hasPerm(['bizApprovalEdit', 'bizApprovalDelete'], 'and')" />
						<a @click="toOpenApproval(record)">审批</a>
						<a-divider type="vertical" v-if="hasPerm(['bizApprovalEdit', 'bizApprovalDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizApproval(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizApprovalDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />

	<a-drawer
		v-model:open="drawerOpen"
		class="custom-class"
		root-class-name="root-class-name"
		:root-style="{ color: 'blue' }"
		title="审批日志"
		placement="right"
	>
		<a-timeline>
			<a-timeline-item
				v-for="(item, index) in approvalLog"
				:index="index"
				:color="item.afterApprovalStatus === 'VISIT_REJECT' ? 'red' : 'green'"
			>
				<div>审批人：{{ item.approvalUserName }}</div>
				<div>审批部门：{{ item.approvalDeptName }}</div>
				<div>审批时间：{{ item.createTime }}</div>
				<div>审批结果： {{ item.afterApprovalStatus === 'VISIT_REJECT' ? '驳回' : '审核通过' }}</div>
			</a-timeline-item>
		</a-timeline>
	</a-drawer>

	<a-modal v-model:open="openApproval" title="审批" @ok="approvalOk">
		<a-form-item label="审批：" name="opinion" disabled>
			<a-select placeholder="请选择审批状态" v-model:value="approvalInfo.status" class="select-approval">
				<a-select-option value="true">通过</a-select-option>
				<a-select-option value="false">驳回</a-select-option>
			</a-select>
		</a-form-item>
		<a-form-item label="备注：" name="opinion" disabled>
			<a-input v-model:value="approvalInfo.opinion" placeholder="请输入备注" allow-clear />
		</a-form-item>
	</a-modal>
</template>

<script setup name="approval">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import bizApprovalApi from '@/api/biz/bizApprovalApi'
	import bizApprovalLogApi from '@/api/biz/bizApprovalLogApi'

	import { message } from 'ant-design-vue'

	const searchFormState = ref({ isApplicant: 'true', approvalStatus: 'APPLICANT_DEPT' })
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()

	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '申请人姓名',
			dataIndex: 'applicantName'
		},
		{
			title: '申请人部门',
			dataIndex: 'applicantDeptName'
		},
		{
			title: '到访部门',
			dataIndex: 'visitDeptName'
		},
		{
			title: '到访午餐食堂',
			dataIndex: 'visitLunchCafeteriaName'
		},
		{
			title: '申请午餐金额',
			dataIndex: 'lunchPrice'
		},
		{
			title: '到访晚餐食堂',
			dataIndex: 'visitSupperCafeteriaName'
		},
		{
			title: '申请晚餐金额',
			dataIndex: 'supperPrice'
		},
		{
			title: '到访时间',
			dataIndex: 'visitTime'
		},
		{
			title: '审批状态',
			dataIndex: 'approvalStatus'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizApprovalEdit', 'bizApprovalDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 250
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		// if (searchFormState.value.approvalStatus == 'APPLICANT_DEPT') {
		// 	searchFormState.value.isApplicant = 'true'
		// } else {
		// 	searchFormState.value.isApplicant = 'false'
		// }
		const searchFormParam = cloneDeep(searchFormState.value)
		if (searchFormParam.visitTime) {
			searchFormParam.startVisitTime = searchFormParam.visitTime[0]
			searchFormParam.endVisitTime = searchFormParam.visitTime[1]
			delete searchFormParam.visitTime
		}
		return bizApprovalApi.bizApprovalPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizApproval = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizApprovalApi.bizApprovalDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchBizApproval = (params) => {
		bizApprovalApi.bizApprovalDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	const approvalStatusOptions = tool.dictList('APPROVAL_STATUS')
	const isApplicantOptions = tool.dictList('IS_TRUE')
	const drawerOpen = ref(false)
	const approvalLog = ref([])

	async function drawerOpenClick(record) {
		const res = await bizApprovalLogApi.bizApprovalLogPage({
			approvalId: record.id,
			sortField: 'createTime',
			sortOrder: 'DESCEND'
		})
		approvalLog.value = res.records
		drawerOpen.value = true
	}

	const openApproval = ref(false)

	function toOpenApproval(record) {
		openApproval.value = true
		approvalInfo.id = record.id
		approvalInfo.status = 'true'
	}

	const approvalInfo = reactive({
		id: '',
		opinion: '',
		status: 'true'
	})

	function approvalOk() {
		bizApprovalApi.approvalOperate(approvalInfo).then(() => {
			message.success('审批成功！！')
			tableRef.value.refresh(true)
			openApproval.value = false
		})
	}
</script>
<style lang="less" scoped>
	.select-approval {
		width: 100%;
	}
</style>
